﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.Office.Interop.Outlook;
using Sync;
using Sync.Outlook;
using ToodleDo;
using ToodleDo.Client;
using Toodledo.Model;
using Toodledo.Model.API;
using Folders = Sync.Outlook.OutlookCategories;


namespace TaskSyncer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {        
        private Syncer _syncer;

        public MainWindow()
        {
            InitializeComponent();
             WindowStartupLocation = WindowStartupLocation.Manual;
            Left = 1920;
            Top = 0;  
            _syncer = new Syncer(tbUser.Text, tbPassword.Text);
            Remote.Init(tbUser.Text, tbPassword.Text, "TaskSyncerOutlook", "api4f07246de3234");
            Logger.MessageEvent += Logger_MessageEvent;            
            btTasks_Click(null,null);
            btAllTasksToodledoo_Click(null, null);            
        }

        void Logger_MessageEvent(string message, Logger.MessageType type)
        {
            switch (type)
            {
                case Logger.MessageType.Tracking:
                    lbDebug.Items.Add(message);
                    break;
                case Logger.MessageType.Error:
                    lbError.Items.Add(message);
                    break;
                default:
                    throw new ArgumentOutOfRangeException("type");
            }
        }
      
        #region private methods
        private void ConfigDatagridToodleDoFolder()
        {
            dgToodleDo.Columns.Clear();
            dgToodleDo.Columns.Add(CreateColumn("Name"));
        }

        private void ConfigDatagridOutlookFolder()
        {            
            dgOutlook.Columns.Clear();
            dgOutlook.Columns.Add(CreateColumn("Name"));
        }

        private void ConfigDatagridOutlookTasks()
        {
            dgOutlook.Columns.Clear();
            dgOutlook.Columns.Add(CreateColumn("Id"));
            dgOutlook.Columns.Add(CreateColumn("Name"));
            dgOutlook.Columns.Add(CreateColumn("Categories"));
            dgOutlook.Columns.Add(CreateColumn("LastUpdate"));

        }

        private void ConfigDatagridToodleDokTasks()
        {
            dgToodleDo.Columns.Clear();
            dgToodleDo.Columns.Add(CreateColumn("Id"));
            dgToodleDo.Columns.Add(CreateColumn("Name"));
            dgToodleDo.Columns.Add(CreateColumn("Modified"));
            
            //dgToodleDo.Columns.Add(CreateColumn("Categories"));
        }
        private static DataGridColumn CreateColumn (string name)
        {
            return new DataGridTextColumn {Header = name, Binding = new Binding(name)};            
        }
        #endregion
        #region Eventhandlers
        private void btOutlookFolders_Click(object sender, RoutedEventArgs e)
        {
            ConfigDatagridOutlookFolder();
            dgOutlook.ItemsSource = Folders.All;
        }
        #endregion

        private void btAllOutlookNonEmptyFolders_Click(object sender, RoutedEventArgs e)
        {
            ConfigDatagridOutlookFolder();
            dgOutlook.ItemsSource = Folders.NonEmptyFolders;
        }

        private void btTasks_Click(object sender, RoutedEventArgs e)
        {
            ConfigDatagridOutlookTasks();
            var x = TasksItems.All;
            dgOutlook.ItemsSource = (from TaskItem taskItem in x select new TaskItemWrapper(taskItem)).ToList();
        }

        private void btToodleFolders_Click(object sender, RoutedEventArgs e)
        {
            ConfigDatagridToodleDoFolder();
            dgToodleDo.ItemsSource = Remote.General.GetFolders();
        }

        private void btSynFolders_Click(object sender, RoutedEventArgs e)
        {
            _syncer.SyncFolders();
        }

        private void btSyncTasks_Click(object sender, RoutedEventArgs e)
        {
            _syncer.SyncTasks();    
            btTasks_Click(null, null);
            btAllTasksToodledoo_Click(null, null);
        }

        private void btAllTasksToodledoo_Click(object sender, RoutedEventArgs e)
        {
            ConfigDatagridToodleDokTasks();
            TaskQuery query = new TaskQuery();                                    
            dgToodleDo.ItemsSource=Remote.Tasks.GetTasks(query).Tasks;
        }

        private void btUpdateSession_Click(object sender, RoutedEventArgs e)
        {
            SyncSessionWrapper.Save();             
        }

        private void btDeleteAllToodleDoTasks_Click(object sender, RoutedEventArgs e)
        {
            TaskQuery query = new TaskQuery();
            foreach (Task task in Remote.Tasks.GetTasks(query).Tasks)
            {
                Remote.Tasks.DeleteTasks(new [] {task.Id});
            }
        }

        private void btSync_Click(object sender, RoutedEventArgs e)
        {
            _syncer.Sync();
            btTasks_Click(null, null);
            btAllTasksToodledoo_Click(null, null);
        }

        private void checkBox1_Checked(object sender, RoutedEventArgs e)
        {
            SetProxy();
        }

        private void SetProxy()
        {
            if (checkBox1.IsChecked != null)                 
                {
                    Connection.Proxy = (bool) checkBox1.IsChecked;
                    Connection.ProxyPassword = tbProxyPW.Text;
                    Connection.ProxyUsername = tbProxyUser.Text;
                    Connection.ProxyPort = Convert.ToInt32(tbProxyPort.Text);
                }
        }

        private void btNewUser_Click(object sender, RoutedEventArgs e)
        {
            _syncer = new Syncer(tbUser.Text, tbPassword.Text);
            Remote.Init(tbUser.Text, tbPassword.Text, "TaskSyncerOutlook", "api4f07246de3234");
            btTasks_Click(null, null);
            btAllTasksToodledoo_Click(null, null);                    
        }
    }
}
